﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using Models.APIModels;
using Models.ServerModels;
using DdnsServer.DnspodAPI;

namespace DdnsServer.BLL
{
    public class RecordBLL
    {
        private DAL.RecordService rCtrl = new DAL.RecordService();
        private DAL.UserService uCtrl = new DAL.UserService();

        #region 更新记录
        public void UpdateRecord(string user_id,string ip)
        {
            if(string.IsNullOrWhiteSpace(user_id) | string.IsNullOrWhiteSpace(ip))
                throw new Exception("参数不能为空！");
            //获取用户信息
            LocalUserModel user = uCtrl.GetUserInfo(user_id);
            if(null==user)
                throw new Exception("用户不存在");
            //获取用户记录
            List<UserRecordListModel> ul = rCtrl.GetUserRecordList(user.id);
            if(null==ul)
                throw new Exception("无记录");
            if(ul.Count<1)
                throw new Exception("无记录");
            //更新
            foreach (var item in ul)
            {
                if (item.last_update_date > DateTime.Now.AddMinutes(-5))
                    continue;//两次更新间隔不到5分钟，跳过
                //上次更新IP
                if (item.last_update_ip == ip)
                    continue;
                //上次更新状态
                if (item.last_update_code == -1 && item.last_update_date > DateTime.Now.AddMinutes(-30))
                    continue;//上次更新状态为登录失败，30分钟内不再重试
                //查询域名信息
                RecordApi ra = new RecordApi(Comm.GetApiCfg(item.api_id, item.api_token));
                var record = ra.GetRecordInfo(item.record_id, item.domain_id);
                if (null == record)
                    continue;
                //不成功的记录日志并更新数据库记录
                if (record.status.code != 1)
                {
                    uCtrl.SaveLog(item.uid, item.sub_domain + "." + item.domain, "code:" + record.status.code.ToString() + " err:" + record.status.message);
                    rCtrl.EditUserRecord(item.id, item.last_update_ip, record.status.code);
                    continue;
                }
                
                if (record.record.value != ip)
                {
                    //更新
                    var updResult = ra.UpdateDdns(ip, item.record_id, item.domain_id, item.domain, item.line, item.line_id, item.sub_domain);
                    //更新数据库记录
                    rCtrl.EditUserRecord(item.id, ip, updResult.status.code);
                    //不成功的记录日志
                    if (updResult.status.code != 1)
                    {
                        uCtrl.SaveLog(item.uid, item.sub_domain + "." + item.domain, "code:" + updResult.status.code.ToString() + " err:" + updResult.status.message);
                        continue;
                    }
                }
                else
                {
                    //与上次IP一样，不更新域名直接更新数据库记录
                    rCtrl.EditUserRecord(item.id, item.last_update_ip, item.last_update_code);
                }
                
            }
        }
        #endregion
    }
}